package cloud.ben.smallscalepigfarmmanagementsystem.service.impl;

import cloud.ben.smallscalepigfarmmanagementsystem.entity.AuthUser;
import cloud.ben.smallscalepigfarmmanagementsystem.entity.User;
import cloud.ben.smallscalepigfarmmanagementsystem.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author: Ben
 * @Date: 2022/3/1 22:33
 */
@Service
public class UserAuthServiceImpl implements UserDetailsService {
    protected static final Logger logger = LoggerFactory.getLogger(UserAuthServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        logger.info("用户名："+userName);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper = queryWrapper.eq("user_name", userName);
        User user= userMapper.selectOne(queryWrapper);
        if (user==null){
            logger.error("用户名不存在,请输入正确的用户名");
            throw new UsernameNotFoundException("用户名不存在,请输入正确的用户名");
        }
        logger.info("用户用户名和密码正确");
        return new AuthUser(user);
    }
}